﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QuanLiSinhVien_v01.DTO;

namespace QuanLiSinhVien_v01.DAO
{
    class BangDiemDao: DataProvider
    {
        public BangDiemDto BangDiem { get; set; }

        protected override Object GetDataFromDataRow(DataTable dt, int rowIndex)
        {
            BangDiemDto BangDiemDto = new BangDiemDto();
            BangDiemDto.MSSV = dt.Rows[rowIndex]["MSSV"].ToString();
            BangDiemDto.MaMonHoc = dt.Rows[rowIndex]["MaMonHoc"].ToString();
            BangDiemDto.LanThu = Int32.Parse(dt.Rows[rowIndex]["LanThu"].ToString());
            BangDiemDto.DiemLiThuyet = float.Parse(dt.Rows[rowIndex]["DiemLiThuyet"].ToString());
            BangDiemDto.DiemThucHanh = float.Parse(dt.Rows[rowIndex]["DiemThucHanh"].ToString());
            BangDiemDto.DiemTongKet = float.Parse(dt.Rows[rowIndex]["DiemTongKet"].ToString());
            BangDiemDto.GhiChu = dt.Rows[rowIndex]["GhiChu"].ToString();
            
            return BangDiemDto;
        }

        public BangDiemDto[] GetList()
        {
            string strCommand = " Select * from BangDiem ";
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            BangDiemDto[] kq = new BangDiemDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (BangDiemDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }

        public void Insert()
        {
            string strCommand =
                "insert into BangDiem (MSSV, MaMonHoc, DiemLiThuyet, DiemThucHanh, DiemKhac, DiemTongKet, GhiChu) " +
                "values ('" + BangDiem.MSSV + "', '" +
                              BangDiem.MaMonHoc + "', " +
                              BangDiem.DiemLiThuyet + ", " +
                              BangDiem.DiemThucHanh + ", " +
                              BangDiem.DiemKhac + ", " +
                              BangDiem.DiemTongKet + ", '" +
                              BangDiem.GhiChu + "'" +
                        ")";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update()
        {
            string strCommand =
                "update BangDiem set " +
                    "LanThu = "+ BangDiem.LanThu+ " , " +
                    "DiemLiThuyet = " + BangDiem.DiemLiThuyet + " , " +
                    "DiemThucHanh = " + BangDiem.DiemThucHanh + " , " +
                    "DiemKhac = " + BangDiem.DiemKhac + " , " +
                    "DiemTongKet = " + BangDiem.DiemTongKet + " , " +
                    "GhiChu = '"   + BangDiem.GhiChu    + "' " +
                 "where MSSV = '" + BangDiem.MSSV + "' " + 
                  " and MaMonHoc ='" + BangDiem.MaMonHoc + "'";
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete()
        {
            string strCommand =
                "DELETE FROM BangDiem " +
                "WHERE  MSSV = '" + BangDiem.MSSV + "' " +
                    "and MaMonHoc = '" +BangDiem.MaMonHoc + "'" ;
            try
            {
                ExcuteNonQuery(strCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public BangDiemDto[] Search(string keywork)
        {
            string strCommand = " Select * from BangDiem " +
                                "where MSSV like '%" + keywork + "%' " +
                                " or MaMonHoc like '%" + keywork + "%' " +
                                " or LanThu = " + keywork;
            DataTable dt = ExcuteQuery(strCommand);
            int n = dt.Rows.Count;
            BangDiemDto[] kq = new BangDiemDto[n];
            for (int i = 0; i < n; i++)
            {
                kq[i] = (BangDiemDto)GetDataFromDataRow(dt, i);
            }
            return kq;
        }
    }
}
